package edu.gym4all.view;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.example.gym4all.R;

import edu.gym4all.service.domain.Training;
import edu.gym4all.service.domain.TrainingPlan;
import edu.gym4all.service.organizer.TrainingOrganizerImpl;
import edu.gym4all.service.organizer.TrainingPlanOrganizerImpl;

public class MainActivity extends Activity {

	private TrainingOrganizerImpl trainingOrganizer = new TrainingOrganizerImpl();

	private TrainingPlanOrganizerImpl trainingPlanOrganizer = new TrainingPlanOrganizerImpl();

	private TrainingPlan trainingPlan;

	private SQLiteDatabase db;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.db = this.trainingPlanOrganizer.initDataBase(this);
		this.setContentView(R.layout.activity_main);

		this.trainingPlan = this.trainingPlanOrganizer.loadTrainingPlan(this);

		List<Training> trainingsArrayAdapter = this.trainingPlanOrganizer
				.loadTrainings(this.trainingPlan);

		ArrayAdapter<Training> trainingList = new ArrayAdapter<Training>(this,
				android.R.layout.simple_list_item_1, trainingsArrayAdapter);
		trainingList.setNotifyOnChange(true);
		ListView listView = (ListView) this.findViewById(R.id.trainingList);
		listView.setAdapter(trainingList);

		Button button = (Button) this.findViewById(R.id.newTraining);
		button.setOnClickListener(new NewTrainingButton(trainingList));

		listView.setOnItemClickListener(new TrainingListener());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		this.getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public TrainingPlan getTrainingPlan() {
		return this.trainingPlan;
	}

	public void setTrainingPlan(TrainingPlan trainingPlan) {
		this.trainingPlan = trainingPlan;
	}

	private class NewTrainingButton implements OnClickListener {

		ArrayAdapter<Training> trainingList;

		public NewTrainingButton(ArrayAdapter<Training> trainingList) {
			this.trainingList = trainingList;
		}

		@Override
		public void onClick(View v) {
			TrainingOrganizerImpl trainingOrganizer = MainActivity.this.trainingOrganizer;
			// TrainingTag tag = MainActivity.this.trainingPlan.getNextTag();
			boolean isFull = trainingOrganizer
					.isTrainingPlanFull(MainActivity.this.trainingPlan);
			if (!isFull) {
				trainingOrganizer.createTraining(
						MainActivity.this.trainingPlan, MainActivity.this,
						MainActivity.this.db);
				this.trainingList.clear();
				for (Training training : MainActivity.this.trainingPlan
						.getTraining()) {
					this.trainingList.add(training);
				}

				this.trainingList.notifyDataSetChanged();
			} else {
				// CRIAR POP UP DE WARNING PARA TREINOS CHEIOS.
			}
		}
	}

	private class TrainingListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			Intent intent = new Intent(MainActivity.this,
					TrainingActivity.class);
			intent.putExtra("training",
					(Training) arg0.getAdapter().getItem(arg2));
			MainActivity.this.startActivity(intent);

		}
	}
}